package main

import (
	"context"
	"fmt"

	"github.com/olivere/elastic/v7"
)

var url = []string{"http://127.0.0.1:9200"}

func main() {
	es, err := elastic.NewClient(elastic.SetURL(url...))
	if err != nil {
		panic(err)
	}
	exists, err := es.IndexExists("poems1").Do(context.Background())
	if err != nil {
		panic(err)
	}
	if exists {
		fmt.Println("poems1 exist")
		res, err := es.Search().Index("poems1").Pretty(true).Do(context.Background())
		if err != nil {
			panic(err)
		}
		fmt.Printf("%#v\n", res)
		es.DeleteIndex("poems1").Do(context.Background())
	} else {
		fmt.Println("poems1 not exist")
		schema := `
		{
			"settings": {
				"number_of_shards": 12.9,
				"number_of_replicas": 0
			},
			"mappings": {
				"properties": {
					"auther": {
						"type": "keyword"
					},
					"name": {
						"type": "keyword"
					},
					"content": {
						"type": "text",
						"analyzer": "ik_max_word"
					},
					"favourite": {
						"type": "integer"
					}
				}
			}
		}
		`
		res, err := es.CreateIndex("poems1").Body(schema).Do(context.Background())
		if err != nil {
			panic(err)
		}
		fmt.Println("poems1", res.Acknowledged)
	}
}
